/*
 * Copyright © 2010 SenseForce.com.
 * The Software shall be used for Good, not Evil.
 */

package com.senseforce.midlets;

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import com.senseforce.classes.MessageConfig;
import com.senseforce.classes.MessageWelcomeForm;
import com.senseforce.classes.MessageEditForm;
import com.senseforce.classes.MessageSendForm;


/**
 * @author jcccn
 * @site http://www.senseforce.com/
 */
public class ShortMessage extends MIDlet {
	private Display display;
	private MessageWelcomeForm welcomeForm;
	private MessageEditForm editForm;
	private MessageSendForm sendForm;
	private Alert alert;
	
    public void startApp() {
		display = Display.getDisplay(this);
		changeFormWithType(MessageConfig.FORMTYPE_WELCOME);
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
    }
	
	public void changeFormWithType(String formType) {
		if(formType == null) {
			return;
		}
		Form toForm = null;
		if (formType.equals(MessageConfig.FORMTYPE_WELCOME)) {
			if (welcomeForm == null) {
				welcomeForm = new MessageWelcomeForm("Welcome!", this);
			}
			toForm = welcomeForm;
		}
		else if (formType.equals(MessageConfig.FORMTYPE_EDIT)) {
			if (editForm == null) {
				editForm = new MessageEditForm("Input a message", this);
			}
			toForm = editForm;
		}
		else if (formType.equals(MessageConfig.FORMTYPE_SEND)) {
			if (sendForm == null) {
				sendForm = new MessageSendForm("Input a mobilephone number", this);
			}
			toForm = sendForm;
		}
		else if (formType.equals(MessageConfig.FORMTYPE_ALERT)) {
			if (alert == null) {
				Image alertImg = null;
				try {
					alertImg = Image.createImage(MessageConfig.img_path_info);
				} catch (Exception e) {
					
				}
				alert = new Alert(null, "Message sent successfully!", alertImg, AlertType.INFO);
				alert.setTimeout(5000);
			}
			if (welcomeForm == null) {
				welcomeForm = new MessageWelcomeForm("Welcome!", this);
			}
			display.setCurrent(alert, welcomeForm);
                        if (editForm != null) {
                            editForm.clearContent();
                        }
                        if (sendForm != null) {
                            sendForm.clearPhoneNumber();
                        }
			return;
		}
		changeForm(toForm);
	}
	
	public void changeForm(Form form) {
		if (form == null) {
			return;
		}
		display.setCurrent(form);
	}
}
